package com.summarecon.qualitycontrol;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.summarecon.database.DatabaseUser;

public class LoginActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //Intent myIntent = new Intent(getBaseContext(), ColorPickerActivity.class);
        //startActivity(myIntent);
        final EditText txtUserName = (EditText)findViewById(R.id.txtNIK);  
        final EditText txtPassword = (EditText)findViewById(R.id.txtPassword);
        
        Button btnLogin = (Button)findViewById(R.id.buttonLogin);  
        btnLogin.setOnClickListener(new OnClickListener(){  
  
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String username = txtUserName.getText().toString();  
                String password = txtPassword.getText().toString();  
                try{  
                    if(username.length() > 0 && password.length() >0)  
                    {  
                        DatabaseUser dbUser = new DatabaseUser(LoginActivity.this);  
                        dbUser.open();  
                        Log.i("login", "" + dbUser.Login(username, password));
                        if(dbUser.Login(username, password))  
                        {  
                            Toast.makeText(LoginActivity.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
                            Intent myIntent = new Intent(getBaseContext(), MenuActivity.class);
                            startActivity(myIntent);
                        }else{  
                            Toast.makeText(LoginActivity.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();  
                        }
                        
                        dbUser.close();  
                    }  
  
                }catch(Exception e)  
                {  
                    Toast.makeText(LoginActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();  
                }  
			}  
  
        }); 
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_login, menu);
        return true;
    }
}
